using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Model;
using WebForum.App_Data;

namespace WebForum
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBoxUserNameLogin.Focus();
            if (Session["user"] != null)
            {
                User u = (User)Session["user"];
                //Response.Redirect("AllForum.aspx");
            }
        }

        /// <summary>
        /// Reg new account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonRegister_Click(object sender, EventArgs e)
        {
            //Save user
            User user = new User();
            user.UserName = TextBoxUserName.Text;
            user.Password = Common.HashMD5(TextBoxPassword.Text);
            user.Email = TextBoxEmail.Text;
            user.Name = TextBoxName.Text;
            user.Age = Common.ParseInt32(TextBoxAge.Text);
            user.Location = TextBoxLocation.Text;
            user.Type = RadioButtonNormalUser.Checked ? BaseForum.Model.User.UserNormal : BaseForum.Model.User.UserDoctor;
            user.PrivateInfo = CheckBoxPrivacy.Checked;
            try
            {
                //user.Id = UserDataSource.Insert(user).Id;
                if (UserDataSource.Insert(user))
                    Session["user"] = user;
                else
                    Common.Show("Insert faild!", Request.UrlReferrer.ToString());
            }
            catch (Exception exception)
            {
                Common.Show(exception.Message, Request.UrlReferrer.ToString());
            }
            //Common.Show("ID of new User"+user.Id,Request.UrlReferrer.ToString());
            //check if doctor=>show doctor info panel
            if (RadioButtonNormalUser.Checked)//Normal User
            {
                PanelRegInfo.Visible = false;
                PanelNormalSuccess.Visible = true;
                LabelUserWellCome.Text = "Hello " + user.Name + "! Welcome to doctor forum!";
            }
            else//Doctor
            {
                PanelRegInfo.Visible = false;
                PanelDoctorSuccess.Visible = true;
                LabelDoctorWellCome.Text = "Hello Doctor " + user.Name + "! Welcome to doctor forum!";
            }
        }
        /// <summary>
        /// Update more doctor info after Register
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButtonMoreDotorInfo_Click(object sender, EventArgs e)
        {
            PanelRegInfo.Visible = false;
            PanelDoctorMoreInfo.Visible = true;
        }
        /// <summary>
        /// Login
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            User u = UserDataSource.Login(Common.RemoveSpecialChars(TextBoxUserNameLogin.Text), Common.HashMD5(TextBoxPasswordLogin.Text));
            if (u == null)
            {
                Common.Show("User Invalid!", Request.UrlReferrer.ToString());
            }
            else
            {
                Session["user"] = u;
                Response.Redirect("AllForum.aspx");
            }
        }
        /// <summary>
        /// Update Doctor Infor
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            Doctor doctor = new Doctor();
            doctor.Id = ((User)Session["user"]).Id;
            doctor.Workplace = TextBoxWorkplace.Text;
            doctor.Specialization = TextBoxSpecialization.Text;
            doctor.Experience = TextBoxExperience.Text;
            doctor.Achievement = TextBoxAchieveme.Text;
            try
            {
                DoctorDataSource.Insert(doctor);
                PanelDoctorMoreInfo.Visible = false;
                PanelDoctorSuccess.Visible = true;
                LinkButtonMoreDotorInfo.Visible = false;
            }
            catch (Exception exception)
            {
                Common.Show(exception.Message, Request.UrlReferrer.ToString());
            }
        }
    }
}
